<?php
/**
 * Created by PhpStorm.
 * User: xutao
 * Date: 2018/8/2
 * Time: 15:06
 */
namespace app\admin\controller;
use app\common\model\LookModel;

class Look extends Base{
    //已通过
    public function index(){
        $where = [];
        if(input('name')){
            $name = input('name');
            $where['m.name'] = ['like',"%$name%"];
        }
        if(input('address')){
            $address = input('address');
            $where['spot'] = ['like',"%$address%"];
        }
        $where['l.state']=1;
        $look = LookModel::alias('l')->join('think_member m','l.member_id = m.id')->where($where)->field('l.*,m.name')->paginate(10);
        $this->assign('look',$look);
        return $this->fetch('index');
    }
    //待审核
    public function look_wait(){
        $where = [];
        if(input('name')){
            $name = input('name');
            $where['m.name'] = ['like',"%$name%"];
        }
        if(input('address')){
            $address = input('address');
            $where['spot'] = ['like',"%$address%"];
        }
        $where['l.state']=0;
        $look = LookModel::alias('l')->join('think_member m','l.member_id = m.id')->where($where)->field('l.*,m.name')->paginate(10);
        $this->assign('look',$look);
        return $this->fetch('wait');
    }
    //删除见闻
    public function del(){
        if(request()->isAjax()){
            if(!input('id')) return json(['code'=>2,'data'=>'','msg'=>'参数错误']);
            $re = LookModel::destroy(input('id'));
            if($re){
                return json(['code'=>200,'data'=>'','msg'=>'删除成功']);
            }else{
                return json(['code'=>1,'data'=>'','msg'=>'删除失败']);
            }
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //获取见闻详情
    public function get_look(){
        if(request()->isAjax()){
            $id = input('id');
            if(!$id) return json(['code'=>1,'data'=>'','msg'=>'参数错误']);
            $re = LookModel::alias('l')->join('think_member m','l.member_id = m.id')->where('l.id',$id)->field('l.*,m.name')->find();
            if($re){
                return json(['code'=>200,'data'=>$re,'msg'=>'ok']);
            }else{
                return json(['code'=>2,'data'=>'','msg'=>'查询失败']);
            }
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //审核
    public function shenghe(){
        if(request()->isAjax()){
            $data = input();
            if(!$data) return json(['code'=>1,'data'=>'','msg'=>'参数错误']);
            //更新状态
            $lookmodel = LookModel::get($data['id']);
            $lookmodel->state = $data['state'];
            $re = $lookmodel->save();
            if($re){
                return json(['code'=>200,'data'=>$lookmodel->state,'msg'=>'更新成功']);
            }else{
                return json(['code'=>2,'data'=>'','msg'=>'更新失败']);
            }
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }

}
